package Code;

import java.util.Arrays;

public class Code189 {
    static void rotate(int[] nums, int k) {
        int n = nums.length;
        k = k % n;
        int[] newNum = new int[n];
        for (int i = 0; i < n; i++) {
            newNum[(i + k) % n] = nums[i];
        }
        System.arraycopy(newNum, 0, nums, 0, n);

    }

    static void reverse(int[] nums, int l, int r) {
        while (l < r) {
            int t = nums[l];
            nums[l] = nums[r];
            nums[r] = t;
            l++;
            r--;
        }
    }

    static void rotate1(int[] nums, int k) {
        int n = nums.length;
        k = k % n;
        reverse(nums, 0, n - 1);
        reverse(nums, 0, k - 1);
        reverse(nums, k, n-1);


    }

    public static void main(String[] args) {
        int[] nums = {1, 2, 3, 4, 5, 6, 7};
        rotate(nums, 3);
        System.out.println(Arrays.stream(nums).boxed().toList());
        rotate1(nums, 1);
        System.out.println(Arrays.stream(nums).boxed().toList());
    }
}
